package org.forten.swt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SwtWin {

	protected Shell shell;
	private Text text;
	private Button button;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SwtWin window = new SwtWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(shell, SWT.SAVE);
				fd.setFilterPath(System.getProperty("JAVA.HOME"));
				fd.setFilterExtensions(new String[] { "*.txt", "*.*" });
				fd.setFilterNames(new String[] { "Text Files(*.txt)",
						"All Files(*.*)" });
				String file = fd.open();
				if (file != null && !file.equals("")) {
					File f = new File(file);
					try (OutputStream os = new FileOutputStream(f);) {
						os.write(text.getText().getBytes());
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		btnNewButton.setBounds(96, 225, 80, 27);
		btnNewButton.setText("保存");

		text = new Text(shell, SWT.BORDER | SWT.V_SCROLL);
		text.setBounds(10, 10, 414, 195);

		button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(shell, SWT.OPEN);
				fd.setFilterPath(System.getProperty("JAVA.HOME"));
				fd.setFilterExtensions(new String[] { "*.txt", "*.*" });
				fd.setFilterNames(new String[] { "Text Files(*.txt)",
						"All Files(*.*)" });
				String file = fd.open();
				if (file != null && !file.equals("")) {
					File f = new File(file);
					byte[] contents = new byte[(int) f.length()];
					try (InputStream is = new FileInputStream(f);) {
						is.read(contents);
						text.setText(new String(contents, "GBK"));
					} catch (IOException ex) {
						System.out.println(ex.getMessage());
					}
				}
			}
		});
		button.setBounds(272, 225, 80, 27);
		button.setText("打开");

	}
}
